package com.lovesher.payment.service.resposity.dao.impl;

import java.util.List;
import java.util.Map;

import com.lovesher.payment.service.resposity.base.DaoDefault;
import com.lovesher.payment.service.resposity.dao.PaymentSerialDao;
import com.lovesher.payment.service.resposity.dao.PluginConfigDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lovesher.payment.common.enums.PayStatus;
import com.lovesher.payment.common.enums.PayType;
import com.lovesher.payment.common.enums.SchedulePayTaskStatus;
import com.lovesher.payment.common.resposity.bean.AccountRechargeDTO;
import com.lovesher.payment.common.resposity.bean.PaymentSerial;
import com.lovesher.payment.common.resposity.bean.TransferDTO;
import com.lovesher.payment.common.resposity.bean.TransferResultDTO;
import com.lovesher.payment.service.resposity.base.DaoDefault;
import com.lovesher.payment.service.resposity.bean.PaymentInterface;
import com.lovesher.payment.service.resposity.dao.PaymentSerialDao;
import com.lovesher.payment.service.resposity.dao.PluginConfigDao;
import com.lovesher.payment.service.resposity.mapper.PaymentSerialMapper;


/**
 * 
 * PaymentSerialDaoImpl
 * 
 **/
@Service("paymentSerialDao")
public class PaymentSerialDaoImpl extends DaoDefault<PaymentSerial, PaymentSerialMapper> implements PaymentSerialDao {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private PaymentSerialMapper paymentSerialMapper;
	
	@Autowired
	private PluginConfigDao pluginConfigDao;

	@Override
	public PaymentSerialMapper getMapper() {
		return paymentSerialMapper;
	}

	@Override
	public PaymentSerial findByOrderSn(String orderSn, Long userId) {
		return paymentSerialMapper.findByOrderSn(orderSn, userId);
	}

	@Override
	public Map<String, String> sendPayReq(AccountRechargeDTO dto) {
		/** 保存交易流水开始 **/
		PaymentSerial paymentSerial = new PaymentSerial();
		paymentSerial.setOrderSn(dto.getOrderSn());//调用第三方流水号
		paymentSerial.setUsername(dto.getUsername());
		paymentSerial.setMoney(dto.getMoney());
		paymentSerial.setFee(dto.getFee());
		paymentSerial.setUserId(dto.getUserId());
		paymentSerial.setTradeNo(dto.getTradeNo());//业务流水号
		paymentSerial.setPaymentMethod(dto.getPayment());
		paymentSerial.setStatus(PayStatus.INIT.getValue());
		paymentSerial.setType(dto.getType());
		paymentSerial.setTaskStatus(SchedulePayTaskStatus.TASK_1000.getValue());
		logger.info(new StringBuffer("PaymentSerialDaoImpl.sendPayReq paymentSerial=").append(JSON.toJSONString(paymentSerial)).toString());
		paymentSerialMapper.insert(paymentSerial);
		PaymentInterface paymentInterface = this.pluginConfigDao.getPaymentPlugin(dto.getPayment());
		/** 保存交易流水结束 **/
		logger.info("交易流水号："+paymentSerial.getOrderSn());
		Map<String, String> reqParams = paymentInterface.getParameterMap(dto, paymentSerial.getOrderSn());
		logger.info(JSON.toJSONString(reqParams));
		return reqParams;
	}

	@Override
	public Boolean hasExist(String tradeNo, Long userId) {
		PaymentSerial paymentSerial = paymentSerialMapper.findByTradeNo(tradeNo, userId);
		if (paymentSerial == null) {
			return false;
		} else {
			return true;
		}
	}


	@Override
	public PaymentSerial findByTradeNo(String tradeNo, Long userId) {
		return paymentSerialMapper.findByTradeNo(tradeNo,userId);
	}

	@Override
	public List<PaymentSerial> getListByStatus(Integer taskStatus, int status, Integer cpos, Integer size,
			List<String> paymentMethods, Integer secTime) {
		return paymentSerialMapper.getListByStatus(taskStatus, status, cpos, size, paymentMethods, secTime);
	}

	@Override
	public TransferResultDTO doTransfer(TransferDTO dto) {

		/** 保存交易流水开始 **/
		PaymentSerial paymentSerial = new PaymentSerial();
		paymentSerial.setOrderSn(dto.getOrderSn());//调用第三方流水号
		paymentSerial.setUsername(dto.getUsername());
		paymentSerial.setMoney(dto.getMoney());
		paymentSerial.setFee(dto.getFee());
		paymentSerial.setUserId(dto.getUserId());
		paymentSerial.setTradeNo(dto.getTradeNo());//业务流水号
		paymentSerial.setPaymentMethod(dto.getPayment());
		paymentSerial.setStatus(PayStatus.INIT.getValue());
		paymentSerial.setType(PayType.TRANSFER.getValue());
		paymentSerial.setTaskStatus(SchedulePayTaskStatus.TASK_1000.getValue());
		logger.info(new StringBuffer("PaymentSerialDaoImpl.doTransfer paymentSerial=").append(JSON.toJSONString(paymentSerial)).toString());
		paymentSerialMapper.insert(paymentSerial);
		PaymentInterface paymentInterface = this.pluginConfigDao.getPaymentPlugin(dto.getPayment());
		/** 保存交易流水结束 **/
		logger.info("交易流水号："+paymentSerial.getOrderSn());
		return paymentInterface.transfer(dto);
	
	}

	@Override
	public PaymentSerial findByOrderSnAndType(String orderSn, Long userId,
			Integer type) {
		return paymentSerialMapper.findByOrderSnAndType(orderSn, userId , type);
	}

}